package ${ftl.packageName}.${cgH.moduleName};
<#assign en =0 />
<#assign ac =3 />
<#assign dao =1 />
<#assign dto =8 />
import com.github.pagehelper.PageInfo;
import ${mList[en].packageName}.${cgH.moduleName}.${tc.entityName}${mList[en].classPostfix};
import ${mList[ac].packageName}.${cgH.moduleName}.${tc.entityName}${mList[ac].classPostfix};
import ${mList[dao].packageName}.${cgH.moduleName}.${tc.entityName}${mList[dao].classPostfix};
import ${mList[dto].packageName}.${cgH.moduleName}.${tc.entityName}${mList[dto].classPostfix};

import java.util.List;
<#if "${cgH.isTree =='1'}">
import com.stars.common.base.BaseConstant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.stars.common.SpecialSymbolUtils;
import org.apache.commons.lang3.StringUtils;
import com.stars.common.base.ResultMsg;
</#if>
import com.stars.entity.ListPage;
import com.github.pagehelper.PageHelper;
import com.stars.services.impl.BaseAccessServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Title: ${tc.tableComment} 业务逻辑
 * @author dj
 * @date ${tc.createTime}
 * @version V1.0
 *
 */
@Service
public class ${tc.entityName}${tc.entityPostfix}  extends BaseAccessServiceImpl<${tc.entityName}${mList[dao].classPostfix} , ${tc.entityName}${mList[en].classPostfix}> implements  ${tc.entityName}${mList[ac].classPostfix} {
    @Autowired
    private ${tc.entityName}${mList[dao].classPostfix}  ${tc.entityName ?uncap_first}${mList[dao].classPostfix};

<#if "${cgH.isTree =='1'}">
    @Override
    public List<${tc.entityName}${mList[dto].classPostfix}> findTreeList(${tc.entityName}${mList[dto].classPostfix} treeDTO) throws Exception {
        List<${tc.entityName}${mList[dto].classPostfix}> treeList = new ArrayList<>();
        if(treeDTO.getNullFlagQuery()){
            treeDTO.setPid(BaseConstant.ROOT);
        }
        treeList = ${tc.entityName ?uncap_first}${mList[dao].classPostfix}.findByTreeList(treeDTO);
        if(treeList.size() < 1){
            return treeList;
        }
        List<${tc.entityName}${mList[dto].classPostfix}> list = ${tc.entityName ?uncap_first}${mList[dao].classPostfix}.findTreeList(treeList);
        return getTree(list);
    }

    private List<${tc.entityName}${mList[dto].classPostfix}> getTree(List<${tc.entityName}${mList[dto].classPostfix}> list) {
        //首先将list转map映射
        Map<String,${tc.entityName}${mList[dto].classPostfix}> map = new HashMap<>();
        List<${tc.entityName}${mList[dto].classPostfix}> treeList = new ArrayList<>();
        //循环生成Tree
        for (${tc.entityName}${mList[dto].classPostfix} dto: list){
            map.put(dto.getId(),dto);
            if(dto.getLayer() < 3){
                dto.setExpanded(true);
            }
            //如果存在父节点，那么获取父节点，并将自己加入父节点
            if(StringUtils.isNoneEmpty(dto.getPid()) && !BaseConstant.ROOT.equals(dto.getPid()) ){
                map.get(dto.getPid()).getChildren().add(dto);
            }else {
                treeList.add(dto);
            }
        }
        return treeList;
    }

</#if>

    /**
     * 分页查询
     * @param pageNum 查询参数 起始页，大小。
     * @return
     */
    @Override
    public PageInfo<${tc.entityName}${mList[dto].classPostfix}> pageList(Integer pageNum, Integer pageSize, ${tc.entityName}${mList[dto].classPostfix} dto) throws Exception{
        ListPage<${tc.entityName}${mList[dto].classPostfix}> listPage = new ListPage<>(pageNum, pageSize, dto);
        PageHelper.startPage(pageNum, pageSize);
        List<${tc.entityName}${mList[dto].classPostfix}> list = ${tc.entityName ?uncap_first}${mList[dao].classPostfix}.pageQuery${tc.entityName}(listPage);
        PageInfo<${tc.entityName}${mList[dto].classPostfix}> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
